﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data;
using Utils.DC.Web.UI;
using Utils.DatabaseUtils;

namespace DCSLibrary
{
    public class CategoryPageUI
    {
        private const String CACHE_KEY = "CategoryPage_GetAll";
        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn,CommandType.StoredProcedure, "CategoryPage_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }


        public DataSet GetByParent(int parentID)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "CategoryPage_GetByParentId", parentID); 
                /*object obj = WebUtility.GetCacheObject(CACHE_KEY + "_Parent_" + parentID);
                if (obj == null)
                {
                    DataSet ds = 
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_Parent_" + parentID, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }*/
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByFunctionType(String FunctionType)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + FunctionType);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "CategoryPage_GetByFunctionType", FunctionType);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + FunctionType, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID(int CategoryPageID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + CategoryPageID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "CategoryPage_get", CategoryPageID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + CategoryPageID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public int DeleteByID(int CategoryPageID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "CategoryPage_delete", CategoryPageID);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        public int Add(String CategoryPageName, String CategoryPageAlias, String CategoryPageTitle, String CategoryPageImage, String CategoryPageIcon,
            String MetaKeyword, String MetaDescription, bool IsVisible, bool AllowDelete, int ViewOrder, bool IsCache, int CacheTime, bool IsGenControlPathByName, String URLPath, String CategoryPageAbstract, String CategoryPageContent, int ParentID, bool IsNew, bool IsTypical, bool isHot)
        {
            try
            {
                
                string ControlPath = GetControlPath(CategoryPageAlias, ParentID,URLPath, IsGenControlPathByName);                

                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "CategoryPage_add", CategoryPageName, CategoryPageAlias, CategoryPageTitle, CategoryPageImage, CategoryPageIcon,
                     MetaKeyword, MetaDescription, IsVisible, AllowDelete, ViewOrder, IsCache, CacheTime, ControlPath, URLPath, CategoryPageAbstract, CategoryPageContent, ((int?)ParentID == 0 ? null : (int?)ParentID), IsNew, IsTypical, isHot);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Add(String CategoryPageName, String CategoryPageAlias, String CategoryPageTitle, String CategoryPageImage, String CategoryPageIcon,
            String MetaKeyword, String MetaDescription, bool IsVisible, bool AllowDelete, int ViewOrder, bool IsCache, int CacheTime, String ControlPath, String URLPath, String CategoryPageAbstract, String CategoryPageContent, int ParentID, bool IsNew, bool IsTypical, bool isHot)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "CategoryPage_add", CategoryPageName, CategoryPageAlias, CategoryPageTitle, CategoryPageImage, CategoryPageIcon,
                     MetaKeyword, MetaDescription, IsVisible, AllowDelete, ViewOrder, IsCache, CacheTime, ControlPath, URLPath, CategoryPageAbstract, CategoryPageContent, ((int?)ParentID == 0 ? null : (int?)ParentID), IsNew, IsTypical, isHot);
                if (i > 0)
                {
                    WebUtility.RemoveCache("FunctionPage_GetAll");
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int CategoryPageID, String CategoryPageName, String CategoryPageAlias, String CategoryPageTitle, String CategoryPageImage, String CategoryPageIcon,
            String MetaKeyword, String MetaDescription, bool IsVisible, bool AllowDelete, int ViewOrder, bool IsCache, int CacheTime, bool IsGenControlPathByName, String URLPath, String CategoryPageAbstract, String CategoryPageContent, int ParentID, bool IsNew, bool IsTypical, bool isHot)
        {
            try
            {
                string ControlPath = GetControlPath(CategoryPageAlias, ParentID, URLPath, IsGenControlPathByName);

                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "CategoryPage_update", CategoryPageID, CategoryPageName, CategoryPageAlias, CategoryPageTitle, CategoryPageImage, CategoryPageIcon,
                     MetaKeyword, MetaDescription, IsVisible, AllowDelete, ViewOrder, IsCache, CacheTime, ControlPath, URLPath, CategoryPageAbstract, CategoryPageContent, ((int?)ParentID == 0 ? null : (int?)ParentID), IsNew, IsTypical, isHot);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + CategoryPageID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int CategoryPageID, String CategoryPageName, String CategoryPageAlias, String CategoryPageTitle, String CategoryPageImage, String CategoryPageIcon,
            String MetaKeyword, String MetaDescription, bool IsVisible, bool AllowDelete, int ViewOrder, bool IsCache, int CacheTime, String ControlPath, String URLPath, String CategoryPageAbstract, String CategoryPageContent, int ParentID, bool IsNew, bool IsTypical, bool isHot)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "CategoryPage_update", CategoryPageID, CategoryPageName, CategoryPageAlias, CategoryPageTitle, CategoryPageImage, CategoryPageIcon,
                     MetaKeyword, MetaDescription, IsVisible, AllowDelete, ViewOrder, IsCache, CacheTime, ControlPath, URLPath, CategoryPageAbstract, CategoryPageContent, ((int?)ParentID == 0 ? null : (int?)ParentID), IsNew, IsTypical, isHot);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + CategoryPageID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Action(String ActionName, String ListID)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(Common.Conn, "CategoryPage_Action", ActionName, ListID);
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        #region Cac Ham Ho Tro

        /// <summary>
        /// Ham lay ve duong dan cua user control
        /// </summary>
        /// <param name="categoryPageAlias">Alias cua trang</param>
        /// <param name="functionPageID">ID cua function page cua trang, dung de lay ve thong tin cua trang chuc nang</param>
        /// <param name="urlPath">Duong dan cua trang, dung de xac dinh level</param>
        /// <returns></returns>
        private string GetControlPath(String categoryPageAlias, int functionPageID, String urlPath, bool IsGenControlPathByName)
        {
            string functionPageAlias = "";
            string functionType = "";
            bool isAdmin = false;

            //lay ve thong tin cua trang chuc nang
            try
            {
                DataRow row = new FunctionPageUI().GetByID(functionPageID).Tables[0].Rows[0];
                functionPageAlias = row["FunctionPageAlias"].ToString().Trim();
                functionType = row["FunctionType"].ToString().Trim();
                isAdmin = Convert.ToBoolean(row["IsAdmin"]);
                functionPageAlias = functionPageAlias.Trim().Replace("-", "_");
            }
            catch (Exception e)
            {
                return "";
            }

            if (isAdmin)
            {
                return "/Admin/Module/" + categoryPageAlias.Replace("-","_") + ".ascx";
            }
            else
            {
                categoryPageAlias = categoryPageAlias.Trim().Replace("-", "_");
                if (IsGenControlPathByName)
                {
                    return "/Module/" + functionType + "/" + categoryPageAlias + ".ascx";
                }
                else
                {
                    //chuyen cac ky tu - cua alias (dung lam url) thanh dau _ de tro thanh ten class HOP LE
                    
                    int categoryLevel = urlPath.Split(new char[] { '/' }).Length - 2;
                    return "/Module/" + functionType + "/" + functionPageAlias + "_category_level_" + categoryLevel + ".ascx";
                }
            }
        }

        #endregion
    }
}
